import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Service } from '../../../server/server'

@Injectable()
export class LocalStorageService {

    constructor(public server :Service) {
        this.userName = this.server.getuserName();
    }
   /**
    * 
    * @param deadLineDate 过期天数
    * @returns {boolean} sss
    */

    public userName:string;

    isVisit(deadLineDate:number):boolean{
        let visitStorage = this.toObj(localStorage.getItem('visit'));
        let userName = this.userName;
        //console.log(userName)
        //console.log(this.userName)
        if(visitStorage && userName == this.userName){
            
            let lastVisitDate = new Date(visitStorage.lastVisitDate)
            let DeadDate = lastVisitDate.setDate((+visitStorage.deadLineDate)+lastVisitDate.getDate());
            let nowDate = new Date()
            let ifDead = DeadDate < nowDate.getTime();
            if(ifDead){
                localStorage.setItem('visit',this.toString({
                    lastVisitDate: new Date(),
                    deadLineDate:deadLineDate
                }))
                return false;
            }else{
                return true;
            }

        }else{
            localStorage.setItem('visit',this.toString({
                lastVisitDate: new Date(),
                deadLineDate:deadLineDate
            }))
            return false
        }
    }
    toString(data:any):string{
        return JSON.stringify(data)
    }
    toObj(data:string):any{
        return JSON.parse(data)
    }
}